import com.sun.org.apache.bcel.internal.generic.ARETURN;

import java.util.Arrays;
import java.util.Scanner;

public class Java8Demo {

    public static void main16(String[] args) {
        int[][] array = new int[2][];//行不能省略，列可以省略
       /* array[0] = new int[2];
        array[1] = new int[4];//不规则二维数组*/
        array[0] = new int[]{11,22,33,44};
        array[1] = new int[]{111,222,456,369,888,7777};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
        /*System.out.println(array3[0]);
        System.out.println(array3[1]);*/
    }

    public static void main15(String[] args) {
        int[][] array = {{1, 2, 3}, {4, 5, 6}};
        /*System.out.println(Arrays.toString(array[0]));
        System.out.println(Arrays.toString(array[1]));*/
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("==================");
        for (int[] ret :array) {
            for (int x : ret) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
        System.out.println("===============");
        System.out.println(Arrays.deepToString(array));
    }

    public static void main14(String[] args) {
        //二维数组
        int[][] array = {{1, 2, 3}, {4, 5, 6}};//2行3列
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("========================");
        int[][] array2 = new int[][]{{1, 2, 3}, {4, 5, 6}};
        int[][] array3 = new int[2][3];
    }

    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main13(String[] args) {
        //数组逆序
        int[] array = {1,2,3,4,5,6};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }

    public static boolean findJshu(int[] array) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 != 0) {
                count++;
            }else if (array[i] % 2 == 0) {
                count = 0;
            }
            if (count == 3) {
                return true;
            }
        }
        return false;
    }

    public static void main12(String[] args) {
        //给你一个整数数组 arr，
        // 请你判断数组中是否存在连续三个元素都是奇数的情况：
        // 如果存在，请返回 true ；否则，返回 false 。
        int[] array = {1,2,34,3,4,5,7,23,12};
        boolean ret = findJshu(array);
        System.out.println(ret);
    }

    public static int findNum(int[] array) {
        //遍历数组
        int ret = array[0];
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (count == 0) {
                count++;
                ret = array[i];
            }else if (array[i] == ret) {
                count++;
            }else if (array[i] != ret) {
                count--;
            }
        }
        return ret;
    }

    public static int find(int[] array) {
        //排序 取中间值
        Arrays.sort(array);
        return array[array.length/2];
    }

    public static void main11(String[] args) {
        //给定一个大小为 n 的数组，找到其中的多数元素。
        //多数元素是指在数组中出现次数 大于(n/2)的元素。
        int[] array = {6,5,5};
        int ret = findNum(array);
        System.out.println(ret);
    }

    public static int fanh(int[] array) {
        int ret = array[0];
        for (int i = 1; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }
    public static void main10(String[] args) {
        //给定一个非空整数数组，除了某个元素只出现一次以外，
        // 其余每个元素均出现两次。找出那个只出现了一次的元素。
        int[] array = {4,2,1,1,2};
        int ret = fanh(array);
        System.out.println(ret);
        System.out.println(4^2);
    }

    public static int[] numTarget(int[] num,int target){
        int[] ret = {-1,-1};
        for (int i = 0; i < num.length; i++) {
            for (int j = i+1; j < num.length; j++) {
                if (num[i] + num[j] == target) {
                    ret[0] = i;
                    ret[1] = j;
                    return ret;
                    //return new int[] {i,j};
                }
            }
        }
        return ret;
    }

    public static void main9(String[] args) {
        //给定一个整数数组 nums和一个整数目标值target，
        // 请你在该数组中找出和为目标值 target的那两个整数，
        // 并返回它们的数组下标。
        int[] num = {2,7,11,15};
        System.out.println(Arrays.toString(numTarget(num, 9)));
    }

    public static void maoPao(int[] array) {
        //此时最外层控制趟数
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            //-i 每一次比上一次少一个比较
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if (flg == false) {
                return;
            }
        }
    }

    public static void main8(String[] args) {
        //判断时间
        int[] array = new int[10_0000];
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
        long start = System.currentTimeMillis();
        maoPao(array);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    public static void main7(String[] args) {
        //冒泡排序（升序）
        int[] array = {21,55,6,8,14,26};
        maoPao(array);
        System.out.println(Arrays.toString(array));
    }

    public static int binarySearch(int[] array,int k) {
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int mid = ((left + right) / 2);
            if (array[mid] < k) {
                left = mid + 1;
            }else if (array[mid] > k) {
                right = mid - 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    public static void main6(String[] args) {
        //二分查找
        int[] array = {11,2,3,4,5,6};
        Arrays.sort(array);//排序
        System.out.println(binarySearch(array, 11));
        //库方法
        System.out.println(Arrays.binarySearch(array, 11));

    }

    public static int xnn(int x,int n) {
        if (n == 1) {
            return x;
        }
        return x * xnn(x,n-1);
    }

    public static void main5(String[] args) {
        //x^n
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入实数x:");
        int x = scanner.nextInt();
        System.out.println("请输入正整数n:");
        int n = scanner.nextInt();
        int a = xnn(x,n);
        System.out.println(a);
    }

    public static void sunx(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            while (left < right && array[left] % 2 != 0) {
                left++;
            }
            while (left < right && array[right] % 2 == 0) {
                right--;
            }
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
        }
    }

    public static void main4(String[] args) {
        //调整数组顺序使得奇数位于偶数之前
        int[] array = {1,2,3,4,5,6};
        sunx(array);
        System.out.println(Arrays.toString(array));
    }

    public static void main3(String[] args) {
        //调整数组顺序使得奇数位于偶数之前
        int[] array = {1,2,3,4,5,6};
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if (array[j] % 2 != 0) {
                    int tmp = array[j];
                    array[j] = array[i];
                    array[i] = tmp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
    }

    public static int[] transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
        return array;
    }

    public static void main2(String[] args) {
        //实现一个方法 transform, 循环将数组中的每个元素乘以2
        int[] array = {1,2,3};
        int[] array1 = transform(array);
        System.out.println(Arrays.toString(array1));
    }

    public static void main1(String[] args) {
        //杨辉三角形
        int level = 7;
        int[][] yh = new int[level][];
        System.out.println("杨辉三角形");
        for (int i = 0; i < yh.length; i++)
            yh[i] = new int[i + 1];
            yh[0][0] = 1;
            for (int i = 1; i < yh.length; i++) {
                yh[i][0] = 1;
                for (int j = 1; j < yh[i].length - 1; j++)
                    yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j];
                yh[i][yh[i].length - 1] = 1;
            }
            for (int[] row : yh) {
                for (int col : row)
                    System.out.print(col + " ");
                System.out.println();
            }
        }
}

